package com.siyuanren.gcocp.service.impl;

import cn.hutool.core.date.DateUtil;
import com.siyuanren.gcocp.dto.HomeworkQuestionDTO;
import com.siyuanren.gcocp.entity.*;
import com.siyuanren.gcocp.enumm.CourseType;
import com.siyuanren.gcocp.mapper.MyWxHomeworkMapper;
import com.siyuanren.gcocp.mapper.base.WxUserChapterMapper;
import com.siyuanren.gcocp.service.HomeworkService;
import com.siyuanren.gcocp.vo.HomeworkAnswerVO;
import com.siyuanren.gcocp.vo.HomeworkCompleteVo;
import com.siyuanren.gcocp.vo.QueryChapterHomeworkVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by   anxing
 * Project_name gcocp-parent
 * Path         com.siyuanren.gcocp.service.impl
 * Date         2018/4/17
 * Time         15:12
 * Description
 */
@Slf4j
@Service
public class HomeworkServiceImpl implements HomeworkService {
    @Autowired
    MyWxHomeworkMapper mapper;
    @Autowired
    WxUserChapterMapper userChapterMapper;

    @Override
    public List<HomeworkQuestionDTO> getChapterHomework(QueryChapterHomeworkVO queryChapterHomeworkVO) throws Exception {
        return mapper.getChapterHomework(queryChapterHomeworkVO);
    }

    @Override
    @Transactional
    public List<HomeworkQuestionDTO> saveAnswerHomework(HomeworkCompleteVo vo, List<HomeworkAnswerVO> list) throws Exception {
        // 提交答案
        for (HomeworkAnswerVO answerVO : list) {
            WxUserHomeworkAnswer answer = new WxUserHomeworkAnswer();
            answer.setUserId(vo.getUserId());
            answer.setChapterId(vo.getChapterId());
            answer.setQuestionId(answerVO.getQuestionId());
            answer.setUserAnswer(answerVO.getAnswer());
            answer.setCreateTime(DateUtil.date());
            mapper.insertSelective(answer);
        }

        // 返回结果
        QueryChapterHomeworkVO queryChapterHomeworkVO = new QueryChapterHomeworkVO();
        queryChapterHomeworkVO.setChapterId(vo.getChapterId());
        queryChapterHomeworkVO.setUserId(vo.getUserId());
        List<HomeworkQuestionDTO> questionList = mapper.getChapterHomework(queryChapterHomeworkVO);
        return questionList;
    }

    @Override
    @Transactional
    public void completeHomework(HomeworkCompleteVo vo) {
        WxUserChapterExample example = new WxUserChapterExample();
        example.or().andUserIdEqualTo(vo.getUserId()).andChapterIdEqualTo(vo.getChapterId());
        List<WxUserChapter> list = userChapterMapper.selectByExample(example);
        if (list.isEmpty()) {
            WxUserChapter chapter= new WxUserChapter();
            chapter.setUserId(vo.getUserId());
            chapter.setCourseId(vo.getCourseId());
            chapter.setChapterId(vo.getChapterId());
            chapter.setHomeworkComplete(1);
            userChapterMapper.insertSelective(chapter);
        } else {
            WxUserChapter chapter = list.get(0);
            chapter.setHomeworkComplete(1);
            userChapterMapper.updateByExampleSelective(chapter, example);
        }
    }
}
